﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

namespace ShenLan.Common
{
    public class SendMailClass
    {
        //站点名称
        private string s_siteName { get; set; }
        //站点地址
        private string s_url { get; set; }
        //技术支持邮箱
        private string s_web_support_email { get; set; }
        //smtp服务器地址
        private string s_smtp { get; set; }
        //smtp发送人邮件地址
        private string s_smtp_address { get; set; }
        //smtp密码
        private string s_smtp_pass { get; set; }

        public SendMailClass()
        { }

        /// <summary>
        /// 发送邮件配置参数
        /// </summary>
        /// <param name="siteName">网站名称</param>
        /// <param name="url">网址</param>
        /// <param name="webSupportEmail">技术支持邮箱</param>
        /// <param name="smtp">smtp服务器地址</param>
        /// <param name="smtpAddress">smtp发送人邮件地址</param>
        /// <param name="smtpPassword">smtp邮件密码</param>
        public SendMailClass(string siteName, string url, string webSupportEmail, string smtp, string smtpAddress, string smtpPassword)
        {
            this.s_siteName = siteName;
            this.s_smtp = smtp;
            this.s_smtp_pass = smtpPassword;
            this.s_smtp_address = smtpAddress;
            this.s_url = url;
            this.s_web_support_email = webSupportEmail;
        }

        /// <summary>
        /// 发送邮件购造函数
        /// </summary>
        /// <param name="mail_to">收件人邮件地址</param>
        /// <param name="mail_title">邮件标题</param>
        /// <param name="mail_body">邮件内容</param>
        /// <returns>true：邮件发送成功，false：邮件发送失败</returns>
        public bool SendMail(string mail_to, string mail_title, string mail_body)
        {
            var client = new System.Net.Mail.SmtpClient();
            client.Host = s_smtp;
            client.UseDefaultCredentials = true;
            client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            client.Credentials = new System.Net.NetworkCredential(s_smtp_address, s_smtp_pass);
            var message = new System.Net.Mail.MailMessage();
            message.From = new System.Net.Mail.MailAddress(s_smtp_address);
            message.To.Add(mail_to);
            message.Subject = mail_title;
            message.Body = mail_body;
            ////附件
            //Attachment item = new Attachment("C:\\Users\\xshai\\Desktop\\会员系统.jpg");
            //message.Attachments.Add(item);
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.Priority = System.Net.Mail.MailPriority.High;
            message.IsBodyHtml = true;           
            try
            {
                client.Send(message);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}